<?php
	/*
	 * Articles_model
	 * @author: Tran Van Thanh
	 * Email  : thanhtran@vietnambiz.com
	 * Date:	06/09/2011
	 */
class Articles_model extends MY_Model{

	function __construct()
	{
		parent::__construct();		
	}
	
	function get_all_articles($limit = 1, $offset = 1)
	{
		$where = array(			
    		'status'			=> 'yes',
    	);		
        
        $q      =       $this->db->from("articles")
                 			 ->where($where)                 			                     
                             ->limit($limit,$offset)
                             ->order_by('date_create DESC')
                             ->get();
        
        $result			= 	array();
        $result['rows'] 	=	$q->result();
        //$q->free_result();

        $q      =	$this->db->select('count(id) as cnt')
                             ->from("articles")                             
                             ->where($where)
                             ->get();
        $result['num_rows']	=	$q->row()->cnt;        
        $q->free_result();        
        return	$result; 
        
        
	}	
	
	// lay ra nhung tin tuyen dung
	function get_all_recruitment($limit = 1, $offset = 1)
	{
		$where = array(			
    		'status'					=> 'yes',
			'news_recruitment'			=> 'yes',
			'news_works'				=> 'no'
    	);		
        
        $q      =       $this->db->from("articles")                 
                 			 ->where($where)                 			                     
                             ->limit($limit,$offset)
                             ->order_by('date_create DESC')
                             ->get();
        
        $result			= 	array();
        $result['rows'] 	=	$q->result();
        $q->free_result();

        $q      =	$this->db->select('count(id) as cnt')
                             ->from("articles")                             
                             ->where($where)
                             ->get();
        $result['num_rows']	=	$q->row()->cnt;
        $q->free_result();
        return	$result; 
	}
	
	function check_email_customers($email)
    {
    	$q  =   $this->db->select()->from("customers")->where('email',$email)->get();
        if($q->row())
        {
        	return true;
        }
        else
        {
        	return false;
		}
	} 	

	function get_articles_detail($id_articles = null)
	{
		$where = array(
			'a.id'		=> $id_articles,
			'a.status'	=> 'yes'
		);
		$q = $this->db->select("a.*")
					 ->from("articles as a")					 					
					 ->where($where)
					 ->order_by("a.date_create DESC")
					 ->get();		
        $result = $q->result();
        $q->free_result();        
        return $result;
	}
	
	function get_articles_orther($id_articles = null)
	{
		$where = array(
			'a.id !=' 				=> $id_articles,
			'a.news_works'			=> 'no',
			'a.news_recruitment'	=> 'no',
			'a.status'				=> 'yes'
		);
		$q = $this->db->select("a.*")
					 ->from("articles as a")
					 ->order_by("a.date_create DESC")					 					
					 ->where($where)
					 ->limit(5)
					 ->get();		
        $result = $q->result();
        $q->free_result();        
        return $result;
	}
	
	function get_articles_orther_recruitment($id_articles = null)
	{
		$where = array(
			'a.id !=' 				=> $id_articles,
			'a.news_works'			=> 'no',
			'a.news_recruitment'	=> 'yes',
			'a.status'				=> 'yes'
		);
		$q = $this->db->select("a.*")
					 ->from("articles as a")
					 ->order_by("a.date_create DESC")					 					
					 ->where($where)
					 ->limit(5)
					 ->get();		
        $result = $q->result();
        $q->free_result();        
        return $result;
	}
	
	function search_articles_by_keywords($keywords = null, $limit = 1, $offset = 1)
	{
		$where = array(			
    		'status'			=> 'yes',
			'news_recruitment'	=> 'no',
			'news_works'		=> 'no'
    	);		
        
        $q      =       $this->db->from("articles")
                 			 ->where($where)      
                 			 ->like('title',$keywords)
                    		 ->or_like('description',$keywords)
                    		 ->or_like('contents',$keywords)           			                     
                             ->limit($limit,$offset)
                             ->order_by('date_create DESC')
                             ->get();
        
        $result			= 	array();
        $result['rows'] 	=	$q->result();
        $q->free_result();

        $q      =	$this->db->select('count(id) as cnt')
                             ->from("articles")                             
                             ->where($where)
                             ->like('title',$keywords)
                    		 ->or_like('description',$keywords)
                    		 ->or_like('contents',$keywords)
                             ->get();
        $result['num_rows']	=	$q->row()->cnt;
        $q->free_result();
        return	$result; 
	}
}